//
//  MainViewController.swift
//  SinaWeibo
//
//  Created by Tony-ztb on 15/12/12.
//  Copyright © 2015年 Tony-ztb. All rights reserved.
//

import UIKit

class MainViewController: UITabBarController {
    
    // 必读属性   因为tabbar是只读属性,所以不能直接设置,要用KVC运行时,间接赋值
    // 必选属性
    let mainTabBar = MainTabBar()
    
    override func viewDidLoad() {
        super.viewDidLoad()

//        print(tabBar.classForCoder)
      
        // 加号按钮的点击事件
        mainTabBar.composeBtn.addTarget(self, action: "composeBtnClick", forControlEvents: .TouchUpInside)
        
        setValue(mainTabBar, forKey: "tabBar")
//        print(tabBar.classForCoder)
        // 添加子视图
        addChildViewControllers()
        
    }
    
    // 加号按钮的点击事件
    @objc private func composeBtnClick() {
        print(__FUNCTION__)
        let composeVC = ComposeViewController()
        let nav = UINavigationController(rootViewController: composeVC)
        // modal出发布视图界面
        presentViewController(nav, animated: true, completion: nil)
        
    }
    

    private func addChildViewControllers() {
        
        // 设置tabbar的渲染颜色
//        self.tabBar.tintColor = themeColor
        
        addChildViewController(HomeTableViewController(), title: "首页", imageName: "tabbar_home")
        addChildViewController(MessageTableViewController(), title: "消息", imageName: "tabbar_message_center")
        addChildViewController(DiscoverTableViewController(), title: "发现", imageName: "tabbar_discover")
        addChildViewController(ProfileTableViewController(), title: "我", imageName: "tabbar_profile")
    }
    
    private func addChildViewController(vc: UIViewController, title: String, imageName: String) {
        
//        let homeVC = HomeTableViewController()
        
        let nav = BaseNavigationViewController(rootViewController: vc)
        // 设置tabbar的名字
        vc.title = title
        // 设置tabbar图片
        vc.tabBarItem.image = UIImage(named: imageName)
        
        addChildViewController(nav)
        
    }
    
    
}
